<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
              <el-input v-model="ruleForm.company"></el-input>
            </el-form-item>
            <el-form-item label="职务" prop="position">
              <el-input v-model="ruleForm.position"></el-input>
            </el-form-item>
            <el-form-item label="参展人数" prop="personal">
              <el-input v-model="ruleForm.personal" type="number" min="0"></el-input>
            </el-form-item>
            <el-form-item label="展位" prop="booth">
              <el-input v-model="ruleForm.booth"></el-input>
            </el-form-item>
            <UploadImg @uploadImg="getUploadImg" :editImg="editImg" :imgTitle="imgTitle"></UploadImg>
            <el-form-item label="状态" prop="state">
              <el-select v-model="ruleForm.state" placeholder="请选择" @change="getChangeState">
                <el-option label="未审核" value="1"></el-option>
                <el-option label="已审核" value="2"></el-option>
                <el-option label="已拒绝" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="展品描述" prop="remark">
              <el-input v-model="ruleForm.remark" type="textarea" :rows="3"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from "../../public/UploadImg";
import checkPhone from "../../common/checkPhone";

export default {
  name: "editexhibition",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        phone: [{ required: true, validator: checkPhone, trigger: "blur" }],
        company: [{ required: true, message: "公司不能为空", trigger: "blur" }],
        position: [{ required: true, message: "职务不能为空", trigger: "blur" }],
        personal: [{ required: true, message: "参展人数不能为空", trigger: "blur" }],
        booth: [{ required: true, message: "展位不能为空", trigger: "blur" }],
        state: [{ required: true, message: "请选择状态", trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        phone: "",
        company:"",
        position:"",
        personal:"",
        booth:"",
        logo: "",
        state:"",
        remark:"",
        projectId: ""
      },
      editImg: "",
      exhibitor_Id: "",
      imgTitle: "上传logo"
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
    var project_Id = localStorage.getItem("project_Id");
    this.projectId = project_Id;
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm =item;
      this.ruleForm.state=item.state.toString();
      this.exhibitor_Id = item.exhibitorId;
      this.editImg = item.logo;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.ruleForm = {
        name: this.ruleForm.name,
        phone: this.ruleForm.phone,
        company: this.ruleForm.company,
        position: this.ruleForm.position,
        personal: this.ruleForm.personal,
        booth: this.ruleForm.booth,
        state: this.ruleForm.state,
        remark: this.ruleForm.remark,
        logo: this.ruleForm.logo,
        projectId: this.projectId
      };
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.exhibitor_Id == "" || this.exhibitor_Id == undefined) {
            this.$axios.post("/Exhibitor/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/Exhibitor/" + this.exhibitor_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.exhibitor_Id = "";
      this.editImg ="";
      this.$refs.ruleForm.resetFields();
    },
    getUploadImg(imgSrc) {
      this.ruleForm.logo = imgSrc;
    },
    getChangeState(value){
        this.ruleForm.state = value
    }
  },
  components: {
    UploadImg
  }
};
</script>